var loginUsername = "${loginUsername}"; // 获得当前登录人员的userName
mLoginUsername = loginUsername;
var partUid = '${request.getParameter("partUid")}';
var date = new Date();
var warehouseId;
var loginUser;

var ioType = '${request.getParameter("ioType")}';

//@Bind #view.onReady
!function(self,arg) {
	loginUser = view.id("dsUser").getData("#");
	
	var ctrlTab = view.id("ctrlTabOut");
	if(ioType==29){
		ctrlTab.set("caption", '一般出库');
	}else if (ioType==27){
		ctrlTab.set("caption", '盘亏出库');
	}else if (ioType==26){
		ctrlTab.set("caption", '检验出库');
	}
	
}

//@Bind @dtIOEmp.onDataChange
!function(self,arg,dsIOEmp,dsInvIO) {
	var ioEmp = dsIOEmp.getData("#");
	//alert(ioEmp.get("cardId"));
	dsIOEmp.set("parameter", {
		cardId : ioEmp.get("cardId")
	}).flush();
	
	var ioEmp = dsIOEmp.getData("#");
	//alert(ioEmp);
	if (ioEmp){
		var invIO = dsInvIO.insert();
		// alert(curEntity.get("dept").get("deptName"));
		if(ioType==29){
			invIO.set("ioType", 29);
			invIO.set("ioTypeDesc", '一般出库');
		}else if (ioType==27){
			invIO.set("ioType", 27);
			invIO.set("ioTypeDesc", '盘亏出库');
		}else if (ioType==26){
			invIO.set("ioType", 26);
			invIO.set("ioTypeDesc", '检验出库');
		}
		
		invIO.set("ioState", 1);
		invIO.set("ioStateDesc", '草稿');	
		invIO.set("ioDate", date);
		invIO.set("deptUid", ioEmp.get("deptUid"));
		invIO.set("dept.deptName", ioEmp.get("dept.deptName"));
		invIO.set("ioEmployeeUid", ioEmp.get("employeeUid"));
		invIO.set("ioEmployee.employeeName", ioEmp.get("employeeName"));
		invIO.set("creatorUid", ioEmp.get("employeeUid"));
		invIO.set("creator.employeeName", ioEmp.get("employeeName"));
		invIO.set("createTime", date);
		invIO.set("warehouseUid", warehouseId);

		var actionGetInvOutBillNumber = view.id("actionGetInvOutBillNumber");
		actionGetInvOutBillNumber.execute(function(result){
			invIO.set("invIOId", result);
		});
	}else {
		dorado.MessageBox.alert("请输入正确的编号...");
	}
	
}


//@Bind #btnAddInvIo.onClick
!function(self,arg) {
	view.id("dsInvIO").clear();
	view.id("dsInvIODetail").clear();
	view.id("dsIOEmp").clear();
	
	var invIO = view.id("dsInvIO").insert();
	var ctrlTab = view.id("ctrlTabOut");

	if(ioType==29){
		invIO.set("ioType", 29);
		invIO.set("ioTypeDesc", '一般出库');
	}else if (ioType==27){
		invIO.set("ioType", 27);
		invIO.set("ioTypeDesc", '盘亏出库');
	}else if (ioType==26){
		invIO.set("ioType", 26);
		invIO.set("ioTypeDesc", '检验出库');
	}
	
	
	invIO.set("ioState", 1);
	invIO.set("ioStateDesc", '草稿');
	invIO.set("ioDate", date);
	invIO.set("createTime", date);
	invIO.set("creatorUid", loginUser.get("employeeUid"));
	invIO.set("creator.employeeName", loginUser.get("employeeName"));
	invIO.set("warehouseUid", warehouseId);
	
	var actionGetInvOutBillNumber = view.id("actionGetInvOutBillNumber");
	actionGetInvOutBillNumber.execute(function(result){
		invIO.set("invIOId", result);
	});
}

//@Bind #btnDelInvIo.onClick
!function(self,arg) {
	view.id("dsInvIO").getData().remove();
	view.id("dsInvIODetail").getData().each(function(entity){
		entity.remove();
	});
	
}

//@Bind #btnAddInvIoDetail.onClick
!function(self,arg) {
	view.id("dsInvIODetail").insert();
	
}

//@Bind #btnDelInvIo.onClick
!function(self,arg) {
	view.id("dsInvIO").getData("#").remove();
	view.id("dsInvIODetail").getData().each(function(entity){
		entity.remove();
	});
	
}

//@Bind #btnDelInvIoDetail.onClick
!function(self,arg) {
	view.id("dsInvIODetail").getData("#").remove();
	
}

//@Bind #deptTree.onDataRowClick
!function(self, arg, dsInvIO) {
	var invIo = dsInvIO.getData("#");
	var curEntity = self.get("currentEntity");
	
	if (curEntity.dataType.get("name") == "dtEmployee") {
		invIo.set("deptUid", curEntity.get("deptUid"));
		invIo.set("dept.deptName", curEntity.get("dept").get("deptName"));
		invIo.set("ioEmployeeUid", curEntity.get("employeeUid"));
		invIo.set("ioEmployee.employeeName", curEntity.get("employeeName"));
		invIo.set("warehouseUid", warehouseId);
		
		view.id('formEmployeeName').refreshData();
		view.id('formDeptName').refreshData();
	}
	dorado.widget.DropDown.findDropDown(self).close();
	
}

// @Bind #dpIoType.beforeExecute
!function(self, arg, dsTypesData) {
	dsTypesData.set("parameter", {
		typeName : "iotype"
	});
	dsTypesData.flush();
}

// @Bind #dgInvInfo.onDataRowClick
!function(self, arg, dsInvIODetail, dsInvInfo) {
	var invInfo = dsInvInfo.getData("#");
	var invIODetail = dsInvIODetail.getData("#");

	invIODetail.set("partDrawingId", invInfo.get("partDrawingId"));
	invIODetail.set("partDescription", invInfo.get("partDescription"));
	invIODetail.set("partName", invInfo.get("partName"));
	invIODetail.set("partUid", invInfo.get("partUid"));
	invIODetail.set("partVersionId", invInfo.get("partVersionId"));
	invIODetail.set("batchnum", invInfo.get("batchNum"));
	invIODetail.set("sn", invInfo.get("sn"));
	invIODetail.set("warebinUid", invInfo.get("warebinUid"));
	view.id("FormwarebinUid").refreshData();
	
	invIODetail.set("warebin.binName", invInfo.get("warebin.binName"));
	invIODetail.set("ioQty", invInfo.get("invQty"));
	invIODetail.set("materialUid", invInfo.get("materialUid"));

	dorado.widget.DropDown.findDropDown(self).close();

}

// @Bind @dtInvIODetail.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = dsInvIO.getData("#");
	var invIODetail = arg.entity;

	invIODetail.set("invIOUid", invIO.get("uniqueId"));
}

// @Bind @dtInvIO.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = arg.entity;
	invIO.set("uniqueId", uuid());
	
}

// @Bind #warehouseTree.onCurrentChange
!function(self, arg) {
	var curEntity = self.get("currentEntity");
	warehouseId = curEntity.get("uuid");

	view.id("dsInvIODetail").clear();
	view.id("dsInvIO").clear();
}

// @Bind #btnQuery.onClick
!function(self, arg, afInvInfo, dsInvInfo) {
	var entity = afInvInfo.get("entity");
	entity.set("warehouseUid", warehouseId);

	dsInvInfo.set("parameter", entity).flush();

}

// @Bind #dsInvInfo.onLoadData
!function(self, arg) {
	self.set("parameter", {
		wareHouseUid : warehouseId
	});
}

// @Bind #dgDetail.onContextMenu
!function(self, arg) {
	view.id("rightKeyMenu").show({
		position : {
			left : arg.event.pageX,
			top : arg.event.pageY
		}
	});

}

// @Bind #menuAdd.onClick
!function(self, dsInvIODetail) {
	var invIODetail = dsInvIODetail.insert();

}

// @Bind #saveRecord.onSuccess
!function(self, arg) {
	var dsInvIO = view.id("dsInvIO");
	var invIO = dsInvIO.getData("#");

	invIO.set("ioState",5);
	invIO.set("ioStateDesc","已登账");
	
}

// @Bind #manuDelete.onClick
!function(self, dsInvIODetail) {
	var invIODetail = dsInvIODetail.getData();

	if (invIODetail.toArray().length > 0) {
		invIODetail.remove();
	} else {
		dorado.MessageBox.alert("没有可删除的数据...");
	}
}

// @Bind #dgDetail.beforeCellValueEdit
!function(self, arg, dsInvInfo) {
	if (arg.column.get("name") == "ioQty"){
		var invInfo = dsInvInfo.getData("#");
		var oldQty = invInfo.get("invQty");
		var newQty = arg.value;
		if (oldQty < newQty){
			dorado.MessageBox.alert("库存数量不足，请重新输入...");
			self.get("currentEntity").set("ioQty", oldQty);
		}
	}
}

//生成uuid
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}